import { getComAttrValue } from './attrs'
import { isObject } from 'lodash-es'
import { BwVueIns } from './model'
import { BwCustomComNodeAttrModel, BwCustomComStructModel } from '../models'

export const getRenderComAttrs = (
  props: Record<string, any>
): Record<string, any> => {
  /** 获取组件属性 */
  const getComAttrs = (
    data: Record<string, BwCustomComNodeAttrModel> | null
  ) => {
    const curAttrs: Record<string, any> = {}
    if (isObject(data)) {
      const vueIns: BwVueIns = props.vueIns
      const scopeData: Record<string, any> = props.scopeData
      Object.keys(data).forEach((key) => {
        const value = getComAttrValue(vueIns, data[key], scopeData)
        curAttrs[key] = value
      })
    }
    return curAttrs
  }

  const nodeInfo: BwCustomComStructModel = props.nodeInfo
  const curAttrs = getComAttrs(nodeInfo.attrs)

  const classList: Array<string> = []
  if (curAttrs.class) {
    classList.push(curAttrs.class)
  }
  const curComProps = {
    ...props,
    ...curAttrs,
    nodeId: nodeInfo.nodeId,
    id: `com_${nodeInfo.nodeId}`,
    class: classList,
  }
  return curComProps
}
